# user_list = ["admin","morning","eight","Mary"]
# pwd_list = ["1234","2234","3234","4234"]
import time
user_list = [{'username':'admin','password':'1234','phone':'15812345678','balance':2349.7,'history':''},
             {'username':'eight','password':'2234','phone':'13526354819','balance':635.9,'history':''}]
login_index = -1

# 判断一个字符串能否被转换为一个有效的数字
def check_number(string):
    point=0
    isvalid=True
    for c in string:
        code=ord(c)
        if (code < 48 and code != 46) or code > 57:
            isvalid = False
            break
        elif code==46:point+=1
    if point>1:isvalid = False
    return isvalid

# 检查一个用户名是否存在，存在返回下标，不存在返回-1
def check_name(name):
    for i in range(0, len(user_list)):
        if user_list[i]['username'] == name:
            return i
    return -1

# 检查手机号是否有效
def check_phone(phone):
    isphone = True
    if len(phone) > 11:
        isphone = False
    for i in range(0,len(phone)):
        if ord(phone[i]) < 48 or ord(phone[i]) > 57:
            isphone = False
            break
    if isphone == False:
        return False
    else:
        return True

# 账户金额的操作
def check_balance(account_index,money):
    user_info = user_list[account_index]
    user_info['balance'] = user_info['balance'] + money
    user_list[account_index] = user_info
    if money < 0:
        print("成功取款%f，剩余金额%f" %(-money,user_info['balance']))
    else:
        print("成功存款%f，剩余金额%f" % (money,user_info['balance']))

# 保存流水记录
def save_history(account_index,op_type,money):
    user_info=user_list[account_index]
    before_histoy = user_info['history']
    this_history = user_info['username'] + ' ' + time.strftime('%Y-%m-%d %H:%M:%S')+ ' ' + op_type + str(money)
    new_history = before_histoy + '##'+this_history
    user_info['history'] = new_history
    user_list[account_index] = user_info


# 注册
def reg():
    name = input("[注册]请输入用户名：")
    pwd = input("[注册]请输入密码：")
    phone = input("[注册]请输入手机号码：")
    if check_name(name) >= 0 or check_phone(phone) == False:
        print("用户信息输入不正确")
    else:
        print("注册成功！")
        user_info = {}
        user_info['username'] = name
        user_info['password'] = pwd
        user_info['phone'] = phone
        user_info['balance'] = 0
        user_info['history'] = ''
        user_list.append(user_info)
        print(user_list)
        start_menu()

# 登录
def login():
    name = input("[登录]请输入用户名：")
    pwd = input("[登录]请输入密码：")
    index = check_name(name)
    if index >= 0:
        if user_list[index]['password'] == pwd:
            print("登录成功")
            global login_index
            login_index = index
            main_menu()
        else:
            print("密码错误")
    else:
        print("用户名错误")

# 查询
def query():
    balance = user_list[login_index]['balance']
    phone = user_list[login_index]['phone']
    print(f'当前余额为：{balance},手机号码为：{phone}')
    main_menu()

# 取款
def withdraw():
    money = input("请输入你的取款金额：")
    if check_number(money) and float(money) <= user_list[login_index]['balance'] and float(money) % 100 == 0:
        check_balance(login_index,float(money) * -1)
        save_history(login_index,'取款', money)
        main_menu()
    else:
        print("取款金额有误")
        withdraw()

# 存款
def deposite():
    money = input("请输入你的存款金额：")
    if check_number(money) and float(money) % 100 == 0:
        check_balance(login_index, float(money))
        save_history(login_index, '存款', money)
    else:
        print("存款金额有误")
        deposite()
    main_menu()

# 转账
def transer():
    account = input("请输入你的转账账号：")
    d = check_name(account)
    if d >= 0 and d != login_index:
        money = input("请输入你的转账金额：")
        if check_number(money):
            money = float(money)
            if money <= user_list[login_index]['balance']:
                user_list[login_index]['balance'] -= money
                user_list[d]['balance'] += money
                print(f"转账成功！当前余额为：{user_list[login_index]['balance']}")
                print(f"另一个账户余额为：{user_list[d]['balance']}")  # 方便检查另一个账户是否有进账
                this_history =  user_list[login_index]['username'] + ' ' + time.strftime('%Y-%m-%d %H:%M:%S')+ ' ' + '转入'+ str(money)
                before_history = user_list[d]['history']
                user_list[d]['history'] = before_history + '##' + this_history
                print(user_list[d]['history'])       # 被转账用户的流水记录
                this_history1 = user_list[login_index]['username'] + ' ' + time.strftime('%Y-%m-%d %H:%M:%S')+ ' ' + '转出' + str(money)
                before_history1 = user_list[login_index]['history']
                user_list[login_index]['history'] = before_history1 + '##' + this_history1
                print(user_list[login_index]['history'])   # 转账后当前用户的流水记录
            else:
                print("余额不足")
                transer()
        else:
            print("你输入的金额不符合规范")
            transer()
    elif d == login_index:
        print("你输入的账号为当前账号")
        transer()
    else:
        print("你输入的账号不存在")
        transer()
    main_menu()

# 流水
def history():
    user_info = user_list[login_index]
    history_list = user_info['history'].split('##')
    for history in history_list:
        print(history)
    main_menu()

# 修改密码
def update_pwd():
    while '1' == '1':
        new_pwd1 = input("请输入新密码：")
        new_pwd2 = input("请重复输入新密码：")
        if new_pwd1 == new_pwd2:
            break
    user_list[login_index]['password'] = new_pwd1
    print("修改密码成功")
    print(user_list[login_index])
    main_menu()

# 开始菜单
def start_menu():
    tip = '''
********************* 欢迎来到蜗牛ATM *********************
********************** 请选择以下操作 *********************
****************** 1.注册  2.登录  3.退卡 *****************         
    '''
    print(tip)
    option = int(input("请输入你的选择："))
    if option == 1:
        reg()
    elif option == 2:
        login()
    elif option == 3:
        exit("退卡成功")

# 主菜单
def main_menu():
    tip = '''
***************************** 进入蜗牛ATM主菜单 *****************************
****************************** 请选择以下操作 *******************************
******************* 1. 查询   2. 取款   3. 存款   4. 转账 *******************
****************** 5. 流水  6. 修改密码   7. 返回   8. 退卡 *****************     
        '''
    print(tip)
    option = int(input("请输入你的选择："))
    if option == 1:
        query()
    elif option == 2:
        withdraw()
    elif option == 3:
        deposite()
    elif option == 4:
        transer()
    elif option == 5:
        history()
    elif option == 6:
        update_pwd()
    elif option == 7:
        start_menu()
    elif option == 8:
        exit('谢谢，欢迎下次光临.')



start_menu()

